<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { submitOrders, payOrders, cancelOrder } from '@/api/order'
import { showToast, showConfirmDialog } from 'vant'

const route = useRoute()
const router = useRouter()
const ticket = ref(JSON.parse(route.query.ticket))
const loading = ref(false)

const buyTime = computed(() => {
  return new Date().toISOString()
})

const handleSubmit = async () => {
  try {
    loading.value = true
    const orderData = {
      ticketId: ticket.value.id,
      buyTime: buyTime.value
    }
    const res = await submitOrders(orderData)
    router.push(`/order/detail/${res.data.orderId}`)
  } catch (error) {
    if (error.response?.data?.message) {
      showToast(error.response.data.message)
    } else {
      showToast('下单失败，请稍后重试')
    }
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="order-create">
    <van-nav-bar title="确认订单" left-arrow @click-left="$router.back()" />
    
    <div class="ticket-info">
      <h2>{{ ticket.ticketName }}</h2>
      <div class="price-info">
        <div class="price">
          <span class="sale-price">¥{{ ticket.salePrice }}</span>
          <span class="original-price">¥{{ ticket.price }}</span>
        </div>
      </div>
      
      <div class="details">
        <div class="detail-item">
          <span class="label">游玩日期</span>
          <span>{{ new Date().toLocaleDateString() }}</span>
        </div>
        <div class="detail-item">
          <span class="label">入园时间</span>
          <span>{{ ticket.enterTime?.split('T')[1].slice(0, 5) }}</span>
        </div>
        <div class="detail-item">
          <span class="label">检票地点</span>
          <span>{{ ticket.enterAddress }}</span>
        </div>
      </div>
    </div>

    <div class="submit-bar">
      <div class="price">
        <span>实付金额：</span>
        <span class="amount">¥{{ ticket.salePrice }}</span>
      </div>
      <van-button type="primary" :loading="loading" @click="handleSubmit">
        提交订单
      </van-button>
    </div>
  </div>
</template>

<style scoped lang="scss">
.order-create {
  min-height: 100vh;
  background: #f8f8f8;
  padding-bottom: 50px;

  .ticket-info {
    background: #fff;
    margin: 12px;
    padding: 16px;
    border-radius: 8px;

    h2 {
      margin: 0 0 16px;
      font-size: 18px;
    }

    .price-info {
      margin-bottom: 16px;
      
      .sale-price {
        color: #ff6b6b;
        font-size: 20px;
        font-weight: bold;
        margin-right: 8px;
      }

      .original-price {
        color: #999;
        text-decoration: line-through;
        font-size: 14px;
      }
    }

    .details {
      .detail-item {
        display: flex;
        justify-content: space-between;
        margin-bottom: 12px;
        font-size: 14px;

        .label {
          color: #666;
        }
      }
    }
  }

  .submit-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 16px;
    background: #fff;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);

    .price {
      .amount {
        color: #ff6b6b;
        font-size: 20px;
        font-weight: bold;
      }
    }
  }
}
</style> 